package com.wwp.tbshop.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.viewpagerindicator.CirclePageIndicator;
import com.wwp.tbshop.R;
import com.wwp.tbshop.util.Constant;
import com.wwp.tbshop.util.UiUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ${wenping} on 6/20/2017.
 */

public class WelcomeAPagerctivity extends Activity {

    @Bind(R.id.vp)
    ViewPager mVp;
    @Bind(R.id.indicator)
    CirclePageIndicator mIndicator;
    @Bind(R.id.start)
    Button mStart;
    /**
     * 图片数据源
     */
    private int[] mImages = {R.mipmap.guide_1, R.mipmap.guide_2, R.mipmap.guide_3};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcomepage);
        ButterKnife.bind(this);

        mVp.setAdapter(mPagerAdapter);
        mIndicator.setViewPager(mVp);
        mVp.addOnPageChangeListener(mOnPageChangeListener);
    }
    @OnClick(R.id.start)
    public void onclick(View view){
        UiUtil.saveBoolean(this, Constant.SPLASH_KEY,true);
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
        finish();//销毁;
    }

    private ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            /**判断
             * 最后一个页面显示按钮*/
            if (position == mImages.length - 1) {
                mStart.setVisibility(View.VISIBLE);
            } else {
                mStart.setVisibility(View.GONE);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };


    /*=============== PagerAdapter ===============*/
    private PagerAdapter mPagerAdapter = new PagerAdapter() {
        @Override
        public int getCount() {
            return mImages.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            //判断标记是否相同
            return view == object;
        }

        /**
         * 实例化 container和imageview
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(WelcomeAPagerctivity.this);
            imageView.setImageResource(mImages[position]);
            /**缩放图片*/
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);

            container.addView(imageView);
            //把标记返回
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //super.destroyItem(container, position, object);
            container.removeView((View) object);
        }
    };
}
